package com.example.job.Activity;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.MenuItem;

import androidx.activity.EdgeToEdge;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.fragment.app.FragmentTransaction;

import com.example.job.R;
import com.example.job.fragment.indexFragment;
import com.example.job.fragment.infoFragment;
import com.example.job.fragment.mineFragment;
import com.google.android.material.bottomnavigation.BottomNavigationView;

public class MainActivity extends AppCompatActivity {

    private indexFragment index;
    private infoFragment info;
    private mineFragment mine;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_main);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });

        BottomNavigationView bottomNavigationView = findViewById(R.id.bottomNavigationView);

        bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                if (item.getItemId() == R.id.index){
                    selectedFragment(0);
                } else if (item.getItemId() == R.id.info) {
                    selectedFragment(1);
                } else{
                    selectedFragment(2);
                }
                return true;
            }
        });

        selectedFragment(0);
    }

    private void selectedFragment(int position) {
        @SuppressLint("CommitTransaction") FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        hideFragment(fragmentTransaction);

        if(position == 0){
            if(index == null){
                index = new indexFragment();
                fragmentTransaction.add(R.id.content,index);
            }else{
                fragmentTransaction.show(index);
            }
        } else if (position == 1) {
            if(info == null){
                info = new infoFragment();
                fragmentTransaction.add(R.id.content,info);
            }else{
                fragmentTransaction.show(info);
            }
        } else{
            if(mine == null){
                mine = new mineFragment();
                fragmentTransaction.add(R.id.content,mine);
            }else{
                fragmentTransaction.show(mine);
            }
        }

        fragmentTransaction.commit();
    }

    private void hideFragment(FragmentTransaction fragmentTransaction){
        if(index!=null){
            fragmentTransaction.hide(index);
        }if(info!=null){
            fragmentTransaction.hide(info);
        }if(mine!=null){
            fragmentTransaction.hide(mine);
        }
    }
}